<?php
class CrawlsController extends AppController{
	
	var $name = 'crawls';
	
// 后台增加 
	function admin_add() {
		$this->pageTitle = __("Add ".$this->modelClass, true);
		
		$modelClass = $this->modelClass;
        if (!empty($this->data)) {
        	$this->data['Article']['user_id'] = $this->currentUser['User']['id'];
        	
            $this->{$modelClass}->create();
            
            if ($this->{$modelClass}->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
                
            }
        }
    }
    // 后台编辑
    function admin_edit($id = null)
    {
    	if (!empty($this->data)) {
    		if(is_array($this->data['Crawl']['category_id']))
    		{
    			$this->data['Crawl']['category_id']=implode(',',$this->data['Crawl']['category_id']);
    		}
    		parent::admin_edit($id);
    	}
    	else
    	{
    		parent::admin_edit($id);
    		if($this->data['Crawl']['category_id'])
    		{
    			$this->data['Crawl']['category_id']=explode(',',$this->data['Crawl']['category_id']);
    		}
    	}
    	
    }
    
    function __dealpages($pages)
    {
    	$pages_array = array();
    	
    	foreach( explode(',',$pages) as $k => $v)
    	{
    		$temparray = explode('-',$v);
    		if(count($temparray)>1)
    		{
    			$start = $temparray[0];
    			$end = $temparray[1];
    			if($temparray[0] > $temparray[1] )
    			{
    				$start = $temparray[0];
    				$end = $temparray[1];
    			}
    			else
    			{
    				$start = $temparray[1];
    				$end = $temparray[0];
    			}
    			for($i = $start;$i >= $end;$i--)
    			{
    				$pages_array[] = $i;
    			}
    		}
    		else
    		{
    			$pages_array[] = $v;
    		}
    	}
    	$pages_array = array_unique($pages_array);
    	rsort($pages_array);
    	return $pages_array;
    }
    
    function admin_crawl($id,$page='0')
    {
    	 $rules = $this->Crawl->read(null, $id);
//    	 print_r($rules);

		 $totalpage = $nextpage = 0;
    	 if($rules['Crawl']['pages'])
		 {
    	 	$pages = $this->__dealpages($rules['Crawl']['pages']);
			$current_page  = $pages[$page];
			$totalpage = count($pages);
		 }
    	 
		 if(strstr($rules['Crawl']['targeturl'],'{page}'))
		 {
		 	$rules['Crawl']['targeturl'] = str_replace('{page}',$current_page,$rules['Crawl']['targeturl']);
		 	$nextpage = ++$page;
		 }
		 
    	 App::import('Core', 'HttpSocket');
    	 $httpsocket = new HttpSocket();
    	 $content = $httpsocket->get($rules['Crawl']['targeturl']);
    	 require_once ROOT.'/app/libs/charset.php';
    	 $content = Charset::gbk_utf8($content);
    	 // 去除html标签，后再匹配链接，防止<a><span></span></a>这类标签不好匹配
//    	 $content = strip_tags($content,'<a><img>');
//    	 $content='href="s d\'">f" sf>sd<span>fjs</a>kl</span></a>';
    	 // [^>|^"|^\'] 指 > " ' 这三个都不能出现
    	 //[^xyz] 	负值字符集合。匹配未包含的任意字符。例如， '[^abc]' 可以匹配 "plain" 中的'p'。
    	 //?号前面有*、+、?时表示非贪婪  ； a?表示a0次或一次  ;  a+?和a*?就表示非贪婪。
    	 $replaceA=array("/","?","'","|",".",);
    	 $replaceB=array('\/','\?',"\'",'\|','\.',);
    	 $rules['Crawl']['urlcontains'] = str_replace($replaceA,$replaceB,$rules['Crawl']['urlcontains']);
    	 $urls = preg_match_all('/<a[^>]*?href=["\']?([^>"\'\s]*'.$rules['Crawl']['urlcontains'].'[^>"\'\s]*)["\']?[^>]*>([\s|\S]*?)<\/a>/ies',$content,$matches);
    	 
    	 $targetModel = 'Article';
    	 $this->loadmodel($targetModel);
    	 $this->data[$targetModel]['user_id'] = $this->currentUser['User']['id'];
    	 $this->data[$targetModel]['status'] = 1;
    	 //print_r($matches);
    	 $replaceA=array("\"","|",);
    	 $replaceB=array('\"','\|',);
    	 if($rules['Crawl']['category_id'])
         {
         	$target_categories =  explode(',',$rules['Crawl']['category_id']);
         	$target_categories = array_unique($target_categories);
         }
         if(empty($matches[1]))
         {
         	echo '抓取参数设置不正确，没有找到对应的内容';exit;
         }
         
         // 数组逆序，排后面的内容是新发布的。抓取的时候排后面的也新发布
         $matches[1] = array_reverse($matches[1],true);
         $matches[2] = array_reverse($matches[2],true);
    	 foreach($matches[1] as $key => $value)
    	 {
    	 	$this->data[$targetModel]['title'] = $matches[2][$key];
    	 	$this->data[$targetModel]['remoteurl'] =  $value;
    	 	
    	 	unset($this->data[$targetModel]['content']);
    	 	unset($this->data[$targetModel]['authorname']);
    	 	unset($this->data[$targetModel]['origin']);
    	 	unset($this->data[$targetModel]['content']);
    	 	
    	 	$content = $httpsocket->get($value);
    	 	$content = Charset::gbk_utf8($content);
    	 	
    	 	if($rules['Crawl']['titlebegin'] && $rules['Crawl']['titleend'])
    	 	{
    	 		$start =  str_replace($replaceA,$replaceB,$rules['Crawl']['titlebegin']);
    	 		$end =  str_replace($replaceA,$replaceB,$rules['Crawl']['titleend']);
    	 		//preg_match('//ies',$content,$innermatches); 
    	 		//echo "|{$start}(.+){$end}|Us";
    	 		if(preg_match("|{$start}(.+){$end}|Us",$content,$innermatches))
    	 		{
    	 			$this->data[$targetModel]['title'] = $innermatches[1];
    	 		}
    	 	}
    	 	if($rules['Crawl']['contentbegin'] && $rules['Crawl']['contentend'])
    	 	{
    	 		$start =  str_replace($replaceA,$replaceB,$rules['Crawl']['contentbegin']);
    	 		$end =  str_replace($replaceA,$replaceB,$rules['Crawl']['contentend']);
    	 		//preg_match('//ies',$content,$innermatches); 
    	 		//echo "|{$start}(.+){$end}|Us";
    	 		if(preg_match("|{$start}(.+){$end}|Us",$content,$innermatches))
    	 		{
    	 			$this->data[$targetModel]['content'] = $innermatches[1];
    	 		}
    	 	}
    	 	$this->{$targetModel}->create();
    	 	
    	 	$this->data[$targetModel]['slug'] = String::uuid();
            //print_r($this->data);
            //exit;
            if ($this->{$targetModel}->save($this->data)) {
            	
            	$this->data['CategoryData']['data_id'] = $this->{$targetModel}->getLastInsertID();
            	$this->data['CategoryData']['type'] = $targetModel;
            	if(!empty($target_categories))
            	{
            		foreach($target_categories as $category_id)
            		{
            			$this->{$targetModel}->CategoryData->create();
		            	$this->data['CategoryData']['category_id'] = $category_id;		            	      	
		            	$this->{$targetModel}->CategoryData->save($this->data);
            		}
            	}
            	
            } 
    	 	//exit;
    	 }
    	 if($nextpage && $totalpage > $nextpage)
    	 {
    	 	return $this->__message('第'.$nextpage.'页',array('action' => 'crawl',$id,$nextpage));
    	 }
    	 else
    	 {
    	 	return $this->__message('已完成',array('action' => 'crawl','action'=>'list'));
    	 }
    	 //$this->redirect(array('action' => 'crawl',$id,$nextpage));
    	 exit;
    }
    
}